package com.g24.partychecker;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

public class WebDBAdapter
{
	private InputStream is;

	public String getAllData(String REGIO) throws Exception
    {
		String result = "";
		
	    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("regio", REGIO));
	
	    //http post
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPost httppost = new HttpPost("http://impulzstudios.com/android/getAllData.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
	 
	    //convert response to string
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	    StringBuilder sb = new StringBuilder();
	    String line = null;
	    while ((line = reader.readLine()) != null) 
	    {
	    	sb.append(line + "\n");
	    }
	    
	    is.close();
	 
	    result = sb.toString();
	  
	    return result;
    }
	
	public String getDataInfo(String ID) throws Exception
    {
		String result = "";
		
		//the year data to send
	    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("naam", ID));
	
	    //http post
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPost httppost = new HttpPost("http://impulzstudios.com/android/getAllData.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
	 
	    //convert response to string
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	    StringBuilder sb = new StringBuilder();
	    String line = null;
	    while ((line = reader.readLine()) != null) 
	    {
	    	sb.append(line + "\n");
	    }
	    
	    is.close();
	 
	    result = sb.toString();
	    
	    return result;
    }
	
	public String getDataUrl(String ID) throws Exception
    {
		String result = "";
		
		//the year data to send
	    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
	    nameValuePairs.add(new BasicNameValuePair("naam", ID));
	
	    //http post
    	HttpClient httpclient = new DefaultHttpClient();
    	HttpPost httppost = new HttpPost("http://impulzstudios.com/android/getAllData.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
	 
	    //convert response to string
	    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
	    StringBuilder sb = new StringBuilder();
	    String line = null;
	    while ((line = reader.readLine()) != null) 
	    {
	    	sb.append(line + "\n");
	    }
	    
	    is.close();
	 
	    result = sb.toString();
	    
	    //parse json data
	    JSONArray jArray = new JSONArray(result);
	    String abc = null;
	    for(int i=0;i<jArray.length();i++)
	    {
	    	JSONObject json_data = jArray.getJSONObject(i);	    	
	    	abc = json_data.getString("website");
	    }
	    return abc;
    }
}